package net.taviscaron.drivenotes.android.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import net.taviscaron.drivenotes.android.R;
import net.taviscaron.drivenotes.android.service.StatisticService;
import net.taviscaron.drivenotes.shared.model.entity.Car;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

public class StatisticActivity extends Activity {
    private static final String TAG = "StatisticActivity";
    private ImageView fuelConsumption;
    private ImageView costDistribution;

    private Bitmap fuelConsumptionBitmap;
    private Bitmap costDistributionBitmap;

    private ProgressDialog progressDialog;

    private Car car;
    private StatisticService statisticService;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.statistic);

	fuelConsumption = (ImageView) findViewById(R.id.fuelConsumption);
	costDistribution = (ImageView) findViewById(R.id.costDistribution);
	car = (Car)getIntent().getSerializableExtra("car");
	statisticService = new StatisticService(this);
    }

    @Override
    protected void onStart() {
	super.onStart();

	if (fuelConsumptionBitmap != null || costDistributionBitmap != null) {
	    setupGraphics();
	} else {
	    new StatisticLoader().execute();
	}
    }

    private void showProgressDialog() {
	progressDialog = ProgressDialog.show(this, null, "Loading...");
    }

    private void closeProgressDialog() {
	progressDialog.dismiss();
    }

    private void setupGraphics() {
	if(fuelConsumptionBitmap != null) {
	    Log.i(TAG, "show fuelConsumptionBitmap");
	    fuelConsumption.setImageBitmap(fuelConsumptionBitmap);
	}
	
	if(costDistributionBitmap != null) {
	    Log.i(TAG, "show costDistributionBitmap");
	    costDistribution.setImageBitmap(costDistributionBitmap);
	}
    }

    private class StatisticLoader extends AsyncTask<Void, Void, Void> {
	@Override
	protected Void doInBackground(Void... args) {
	    String[] statistic = statisticService.loadStatistic(car);
	    String fuelConsumptionGraphicUrl = statistic[0];
	    String costDistributionGraphicUrl = statistic[1];
	    Log.i(TAG, "fuelConsumptionGraphicUrl: " + fuelConsumptionGraphicUrl);
	    Log.i(TAG, "costDistributionGraphicUrl: " + costDistributionGraphicUrl);
	    
	    if(fuelConsumptionGraphicUrl.length() > 0) {
		fuelConsumptionBitmap = loadImage(fuelConsumptionGraphicUrl);
	    }
	    if(costDistributionGraphicUrl.length() > 0) {
		costDistributionBitmap = loadImage(costDistributionGraphicUrl);
	    }
	    return null;
	}

	private Bitmap loadImage(String stringUrl) {
	    InputStream is = null;
	    try {
		URL url = new URL(stringUrl);
		is = url.openStream();
		Bitmap bitmap = BitmapFactory.decodeStream(is);
		return bitmap;
	    } catch (IOException e) {
		Log.e(TAG, "Error: " + e);
		return null;
	    } finally {
		if (is != null) {
		    try {
			is.close();
		    } catch (IOException e) {
		    }
		}
	    }
	}
	
	@Override
	protected void onPreExecute() {
	    showProgressDialog();
	    Log.i(TAG, "start loading");
	}
	
	@Override
	protected void onPostExecute(Void result) {
	    closeProgressDialog();
	    Log.i(TAG, "show images");
	    if(fuelConsumptionBitmap == null && costDistributionBitmap == null) {
		Toast.makeText(StatisticActivity.this, "Insufficient charging and service data to perform analysis", Toast.LENGTH_LONG).show();
	    } else if(fuelConsumptionBitmap == null) {
		Toast.makeText(StatisticActivity.this, "Insufficient charging data to perform analysis", Toast.LENGTH_LONG).show();
	    } else if(costDistributionBitmap == null) {
		Toast.makeText(StatisticActivity.this, "Insufficient charging and service data to perform analysis", Toast.LENGTH_LONG).show();
	    }
	    setupGraphics();
	}
    }
}
